package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.IMobileClientDAO;
import org.jsbd.boss.domian.channel.Hsman;
import org.jsbd.boss.domian.channel.MobileClient;
import org.jsbd.boss.service.IDeviceService;
import org.jsbd.boss.service.IHsmanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

/**
 * 
 * 手机机型管理
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/device")
public class DeviceController {
	public static final Logger LOGGER = LoggerFactory.getLogger(DeviceController.class);

	@Resource
	private IDeviceService deviceService;

	@Resource
	private IHsmanService hsmanService;
	
	@Resource
	private IMobileClientDAO mobileClientDAO;

	@RequestMapping(value = "deviceList", method = RequestMethod.GET)
	public void selectVers(HttpServletResponse response, String verType, String mark) {
		try {
			List<Hsman> list = deviceService.selectList(mark);
			ResponseUtils.renderJson(response, JSONArray.toJSONString(list));
		} catch (Exception e) {
			LOGGER.error("获取设备信息失败", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "device/list";
	}

	@RequestMapping(value = "list", params = "json")
	@ResponseBody
	public Map queryList(Hsman hsman, Model model) throws Exception {
		Map map = new HashMap<String, List<Hsman>>();
		List<Hsman> list = hsmanService.selectList(hsman);
		map.put("rows", list);
		map.put("total", hsmanService.selectCount(hsman));
		return map;
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, Model model) {
		if (id != null) {
			Hsman hsman = hsmanService.selectHsman(id);
			if (hsman != null) {
				model.addAttribute("hsman", hsman);
			}
		}
		return "device/update";
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public void update(HttpServletResponse response, Hsman hsman) {
		try {
			hsmanService.updateHsman(hsman);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
	public void del(HttpServletResponse response, @PathVariable String id) {
		try {
			String[] ids = id.split(",");
			for(String strId : ids){
				hsmanService.deleteHsman(Integer.parseInt(strId));
			}
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "device/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void add(HttpServletResponse response, Hsman hsman) {
		try {
			String hsmanStr = hsman.getHsman();
			if(StringUtils.isNotBlank(hsmanStr)){
				List<MobileClient> list = mobileClientDAO.selectMobileType(hsmanStr);
				List<Hsman> hsmanList = new ArrayList<Hsman>();
				if(list != null){
					for(MobileClient client : list){
						Hsman temp = new Hsman();
						temp.setHsman(hsmanStr);
						temp.setHstype(client.getHstype());
						hsmanList.add(temp);
					}
				}
				hsmanService.batchInsert(hsmanList);
			}
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseInfoExists(response);
		}
	}
}
